package com.example.demo;

import ch.qos.logback.core.util.StringUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    private List<MessageInfo> messageInfos=new ArrayList<>();
    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        return messageInfos;
    }
    @RequestMapping(value = "/publish", produces = "application/json")
    public String publish(@RequestBody MessageInfo messageInfo){
        //参数校验
        if(StringUtil.isNullOrEmpty(messageInfo.getFrom())
                ||StringUtil.isNullOrEmpty(messageInfo.getTo())
                ||StringUtil.isNullOrEmpty(messageInfo.getMsg())){
            return "{\"ok\":0}";
        }
        messageInfos.add(messageInfo);
        return "{\"ok\":1}";
    }
}
